<?php
	function sendMail($body, $subject, $mail)
	{
		echo sendThisEmail($body, $subject, '', '', $mail, 'Sole Service Order Form <orders@soleservice.com.ph>', true);
	//ACTIVATE EMAILER HERE
	}
	function modifyOrder()
	{
		if(get('orderstat') !== 'null')
		{
			$tables = array();
			$tables[0] = 'ex_pending_orders';
			
			$fields = array();
			$fields[0] = 'orderID';
			
			$whereClause = 'orderID > -1 LIMIT 1';
			
			$data = SQLRecover($tables, $fields, $whereClause);
			
/* 			echo $data['queryStatus'].'<br />'.$data['traceSQL']; */
			
			if($data['queryStatus'] == 'DB Query success!')
			{
				if(get('orderstat') == 'done')
				{
					$tableName = 'ex_pending_orders';
					
					$fields = array();
					$fields[0] = 'imgFile';
					
					$values = array();
					$values[0] = get('waybill');
					
					$whereClause = 'orderID="'.get('orderID').'"';
					
					$data = SQLUpdate($tableName, $whereClause, $fields, $values);
					
					$tables = array();
					$tables[0] = 'ex_pending_orders';
					
					$fields = array();
					$fields[0] = 'hash';
					$fields[1] = 'memberID';
					
					$whereClause = 'orderID="'.get('orderID').'"';
					
					$data = SQLRecover($tables, $fields, $whereClause);
					
					$tables = array();
					$tables[0] = 'ex_shoppers';
					
					$fields = array();
					$fields[0] = 'email';
					$fields[1] = 'name';
					
					$whereClause = 'memberID = "'.$data['memberID'][0].'"';
					
					$dataZ = SQLRecover($tables, $fields, $whereClause);
					
					$namer = explode(', ', $dataZ['name'][0]);
					
					$orderID = $data['hash'][0].'zdghd'.get('orderID');
					
					sendMail('Mr./Ms. '.$namer[1].' '.$namer[0].'<br /><br />Your payment has been previously confirmed and your order has been shipped. You can view your current order statistics and track your order delivery status <a href="'.INDEX.'orderSent'.EXT.'orderID='.$orderID.'" target="_blank"><b>here</b></a>.<br /><br />Thank you for shopping! <br /><br />This is an automated email service. Please do not reply to this email. Send your inquiries to inquiry@soleservice.com.ph instead. Thank you!', 'Your order has been shipped and is en route to your delivery option.', $dataZ['email'][0]);
					
				}
				else if(get('orderstat') == 'del')
				{
					//GET VALUES CURRENT PENDING ORDER TO BE DELETED
					$tables = array();
					$tables[0] = 'ex_pending_orders';
					
					$fields = array();
					$fields[0] = '*';
					
					$whereClause = 'orderID="'.get('orderID').'"';
					
					$data = SQLRecover($tables, $fields, $whereClause);
					if($data['queryStatus'] == 'DB Query success!')
					{
						//DECIDE WHAT TO PUT IN IMGFILE
						$xz = '';
						
						if(file_exists(CM_ROOTDIR.'img/a1ow17n1y437bawg42147/'.$data['imgFile'][0]))
						{
							unlink(CM_ROOTDIR.'img/a1ow17n1y437bawg42147/'.$data['imgFile'][0]);
							$xz = 'Order was unpaid but was confirmed with deposit slip.';
						}
						else
						{
							if($data['imgFile'][0] == 'noImg.jpg')
							{
								$xz = 'Order cancelled.';
							}
							else if($data['imgFile'][0] == 'paid')
							{
								$xz = 'Order was considered paid but not shipped.';
							}
							else
							{
								$xz = 'Order was shipped/picked up.';
							}
						}
						
						//INSERT NEW ROW IN ARCHIVE DOCUMENTING DELETION
						$tableName = 'ex_orders_archive';
						
						$fields = array();
						$fields[0] = 'orderID';
						$fields[1] = 'orderDetails';
						$fields[2] = 'memberID';
						$fields[3] = 'expiry';
						$fields[4] = 'hash';
						$fields[5] = 'imgFile';
						
						$values = array();
						$values[0] = $data['orderID'][0];
						$values[1] = addslashes($data['orderDetails'][0]);
						$values[2] = $data['memberID'][0];
						$values[3] = $data['expiry'][0];
						$values[4] = $data['hash'][0];
						$values[5] = $xz;
						
						$data = SQLInsert($tableName, $fields, $values);
						//DELETE CURRENT ORDER MARKED DELETE
						$data = SQLDelete ('ex_pending_orders', 'orderID = "'.get('orderID').'"');
					}
					
					//REDIR TO PREVENT DOUBLE ACTIONS
					echo redirectSiteNow(INDEX.'orderList');
				}
				else if(get('orderstat') == 'paid')
				{
					$tables = array();
					$tables[0] = 'ex_pending_orders';
					
					$fields = array();
					$fields[0] = 'hash';
					$fields[1] = 'memberID';
					
					$whereClause = 'orderID="'.get('orderID').'"';
					
					$data = SQLRecover($tables, $fields, $whereClause);
					
					$tables = array();
					$tables[0] = 'ex_shoppers';
					
					$fields = array();
					$fields[0] = 'email';
					$fields[1] = 'name';
					
					$whereClause = 'memberID = "'.$data['memberID'][0].'"';
					
					$dataZ = SQLRecover($tables, $fields, $whereClause);
					
					$namer = explode(', ', $dataZ['name'][0]);
					
					$orderID = $data['hash'][0].'zdghd'.get('orderID');
					
					
					sendMail('Mr./Ms. '.$namer[1].' '.$namer[0].'<br /><br />Your payment has been confirmed and your order is ready to be shipped. You can view your current order statistics <a href="'.INDEX.'orderSent'.EXT.'orderID='.$orderID.'" target="_blank"><b>here</b></a>.<br /><br />Thank you for shopping! <br /><br />This is an automated email service. Please do not reply to this email. Send your inquiries to inquiry@soleservice.com.ph instead. Thank you!', 'Payment Confirmed on your order at www.soleservice.com.ph', $dataZ['email'][0]);			
				
					$tableName = 'ex_pending_orders';
					$tables = array();
					$tables[0] = $tableName;
					
					$fields = array();
					$fields[0] = 'imgFile';
					
					$values = array();
					$values[0] = 'paid';
					
					$whereClause = 'orderID = "'.get('orderID').'"';
					
					$dataX = SQLRecover($tables, $fields, $whereClause);
					
					$data = SQLUpdate($tableName, $whereClause, $fields, $values);
					
					if(file_exists(CM_ROOTDIR.'img/a1ow17n1y437bawg42147/'.$dataX['imgFile'][0]))
					{
						unlink(CM_ROOTDIR.'img/a1ow17n1y437bawg42147/'.$dataX['imgFile'][0]);
					}
					echo redirectSiteNow(INDEX.'orderList');
				}
				else if(get('orderstat') == 'extend')
				{
					//EMAIL THE CLIENT HERE!
					
					//GET CURRENT ENTRY VALUES: expiry
					$tables = array();
					$tables[0] = 'ex_pending_orders';
					
					$fields = array();
					$fields[0] = 'expiry';
					$fields[1] = 'memberID';
					$fields[2] = 'hash';
					
					$whereClause = 'orderID="'.get('orderID').'"';
					
					$data = SQLRecover($tables, $fields, $whereClause);
					
					//GET SHOPPER ACCOUNT
					$tables = array();
					$tables[0] = 'ex_shoppers';
					
					$fields = array();
					$fields[0] = 'email';
					$fields[1] = 'name';
					
					$whereClause = 'memberID = "'.$data['memberID'][0].'"';
					
					$dataZ = SQLRecover($tables, $fields, $whereClause);
					
					$namer = explode(', ', $dataZ['name'][0]);
					
					$orderID = $data['hash'][0].'zdghd'.get('orderID');
					
					$origDate = (ifDataExists('ex_extension_tracking', 'orderID="'.get('orderID').'"') ? getData('ex_extension_tracking', 'orderDate', 'extendedTo') : $data['expiry'][0]);
					
					//COMPUTE FOR DAYS EXT
					$dateExt = date('Y-m-j', strtotime('+'.get('extend').' day', strtotime($data['expiry'][0])));
					$parseDate = dateParser(explode('-', $dateExt));
					
					sendMail('Mr./Ms. '.$namer[1].' '.$namer[0].'<br /><br />The expiry date of your recent order with order number '.$orderID.' was extended to '.$parseDate.'. You can view your current order statistics <a href="'.INDEX.'orderSent'.EXT.'orderID='.$orderID.'" target="_blank"><b>here</b></a>. Please make sure that all details in your deposit slip are legible to prevent problems with your shipping.<br /><br />Thank you for shopping! <br /><br />This is an automated email service. Please do not reply to this email. Send your inquiries to inquiry@soleservice.com.ph instead. Thank you!', 'Expiry date extended on your order at www.soleservice.com.ph', $dataZ['email'][0]);		
					
					//UPDATE WITH NEW VALUES
					$tableName = 'ex_pending_orders';
					
					$fields = array();
					$fields[0] = 'expiry';
					
					$values = array();
					$values[0] = $dateExt;
					
					$whereClause = 'orderID = "'.get('orderID').'"';
					
					$data = SQLUpdate($tableName, $whereClause, $fields, $values);
					
					//ARCHIVE CHANGES
					$fields = array();
					$fields[0] = 'orderDate';
					$fields[1] = 'extendedTo';
					$fields[2] = 'orderID';
					
					$values = array();
					$values[0] = $origDate;
					$values[1] = $dateExt;
					$values[2] = get('orderID');
					
					insertUpdate('ex_extension_tracking', 'orderID="'.get('orderID').'"', $fields, $values);
					
					//REDIRECT SITE.
/* 					echo redirectSiteNow(INDEX.'orderList'); */
				}
				else if(get('orderstat') == 'reset')
				{
					//EMAIL THE CLIENT HERE!
					
					
					
					//GET CURRENT ENTRY VALUES: expiry/imgfile
					$tables = array();
					$tables[0] = 'ex_pending_orders';
					
					$fields = array();
					$fields[0] = 'expiry';
					$fields[1] = 'imgFile';
					$fields[2] = 'memberID';
					$fields[3] = 'hash';
					
					$whereClause = 'orderID = "'.get('orderID').'"';
					
					$data = SQLRecover($tables, $fields, $whereClause);
					
					$tables = array();
					$tables[0] = 'ex_shoppers';
					
					$fields = array();
					$fields[0] = 'email';
					$fields[1] = 'name';
					
					$whereClause = 'memberID = "'.$data['memberID'][0].'"';
					
					$dataZ = SQLRecover($tables, $fields, $whereClause);
					
					$namer = explode(', ', $dataZ['name'][0]);
					
					$orderID = $data['hash'][0].'zdghd'.get('orderID');
					
					$origDate = (ifDataExists('ex_extension_tracking', 'orderID="'.get('orderID').'"') ? getData('ex_extension_tracking', 'orderDate', 'extendedTo') : $data['expiry'][0]);
					
					sendMail('Mr./Ms. '.$namer[1].' '.$namer[0].'<br /><br />There was a problem regarding the deposit slip image submitted earlier in order number '.$orderID.'. The deposit slip image was not legible for confirmation. Please upload another scanned copy of your deposit slip <a href="'.INDEX.'orderSent'.EXT.'orderID='.$orderID.'" target="_blank"><b>here</b></a>. Please make sure that all details are legible to prevent this from happening again.<br /><br />We apologize for the inconvenience. <br /><br />This is an automated email service. Please do not reply to this email. Send your inquiries to inquiry@soleservice.com.ph instead. Thank you!', 'There was an issue regarding your order at www.soleservice.com.ph', $dataZ['email'][0]);
					
					//DELETE CURRENT UPLOADED IMG
					if(file_exists(CM_ROOTDIR.'img/a1ow17n1y437bawg42147/'.$data['imgFile'][0]))
					{
						unlink(CM_ROOTDIR.'img/a1ow17n1y437bawg42147/'.$data['imgFile'][0]);
					}
					//COMPUTE FOR # of DAYS LATER
					$dateExt = date('Y-m-j', strtotime('+'.get('extend').' day', strtotime($data['expiry'][0])));
					
					//UPDATE ENTRY
					$tableName = 'ex_pending_orders';
					
					$fields = array();
					$fields[0] = 'expiry';
					$fields[1] = 'imgFile';
					
					$values = array();
					$values[0] = $dateExt;
					$values[1] = 'noImg.jpg';
					
					$whereClause = 'orderID = "'.get('orderID').'"';
					
					$data = SQLUpdate($tableName, $whereClause, $fields, $values);
					
					//ARCHIVE CHANGES
					$fields = array();
					$fields[0] = 'orderDate';
					$fields[1] = 'extendedTo';
					$fields[2] = 'orderID';
					
					$values = array();
					$values[0] = $origDate;
					$values[1] = $dateExt;
					$values[2] = get('orderID');
					
					insertUpdate('ex_extension_tracking', 'orderID="'.get('orderID').'"', $fields, $values);
					
					
					//REDIRECT SITE.
					echo redirectSiteNow(INDEX.'orderList');
				}
				else if(get('orderstat') == 'resetArchive')
				{
					//MOVE FROM ARCHIVE TO ACTIVE DB
				}
				else
				{
					//doNothing();
				}

			}
		}
	}
	
	function cleanUpOrdersList()
	{
		$tables = array();
		$tables[0] = 'ex_pending_orders';
		
		$fields = array();
		$fields[0] = '*';
		
		$whereClause = 'expiry < "'.sqlDateNow().'" AND imgFile = "noImg.jpg"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		
		if($data['queryStatus'] == 'DB Query success!')
		{
			for ($i=0; $i<sizeOf($data['orderID']); $i++)
			{
				$tableName = 'ex_orders_archive';
				
				$fields = array();
				$fields[0] = 'orderID';
				$fields[1] = 'orderDetails';
				$fields[2] = 'memberID';
				$fields[3] = 'expiry';
				$fields[4] = 'hash';
				$fields[5] = 'imgFile';
				
				$values = array();
				$values[0] = $data['orderID'][$i];
				$values[1] = addslashes($data['orderDetails'][$i]);
				$values[2] = $data['memberID'][$i];
				$values[3] = $data['expiry'][$i];
				$values[4] = $data['hash'][$i];
				$values[5] = 'Order expired.';
					
				$dataG = SQLInsert($tableName, $fields, $values);
				
				$dataF = SQLDelete ('ex_pending_orders', 'orderID="'.$data['orderID'][$i].'"');
			}
		}
	}
	function getMemberEmail($id)
	{
		$tables = array();
		$tables[0] = 'ex_shoppers';
		
		$fields = array();
		$fields[0] = 'email';
		
		$whereClause = 'memberID="'.$id.'"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		
		return $data['email'][0];
	}
	function table_ize($dataArray)
	{
		if(!$dataArray)
		{
			//doNothing();
			return '<div class="centerAlign htop">
				There are no '.(get('archive') == 'null' ? 'active' : 'inactive').' records found.
			</div>';
		}
		else
		{
			
			$return = '';
			for ($i=0; $i<sizeOf($dataArray['orderID']); $i++)
			{
				$dateOnCall = (ifDataExists('ex_extension_tracking', 'orderID = "'.$dataArray['orderID'][$i].'"') ? getData('ex_extension_tracking', 'orderDate', 'orderID="'.$dataArray['orderID'][$i].'"') : $dataArray['expiry'][$i]);
				
				if($dataArray['imgFile'][$i] == 'noImg.jpg')
				{
					//PENDING ORDERS
					$resetLink = '[<a class="orderExtendLink" href="'.INDEX.'404" alt="'.INDEX.'orderList'.EXT.'orderstat=extend&orderID='.$dataArray['orderID'][$i].'"><b>Extend Expiry</b></a>][<a class="orderDeleteLink" href="'.INDEX.'orderList'.EXT.'orderstat=del&orderID='.$dataArray['orderID'][$i].'"><b>Cancel</b></a>]';
					$imgLink = 'Pending. Expires on '.dateParser(explode('-', $dataArray['expiry'][$i]));
				}
				else if($dataArray['imgFile'][$i] == 'paid')
				{
					//PAID NOT YET CONFIRMED NOT YET SHIPPED ORDERS
					$resetLink = '[<a href="'.INDEX.'404" class="orderShippedLink" alt="'.INDEX.'orderList'.EXT.'orderstat=done&orderID='.$dataArray['orderID'][$i].'"><b>Order Shipped</b></a>] [<a class="orderDeleteLink" href="'.INDEX.'orderList'.EXT.'orderstat=del&orderID='.$dataArray['orderID'][$i].'"><b>Cancel</b></a>]';
					$imgLink = 'Paid. Ready for Shipping.';
				}
				else if($dataArray['imgFile'][$i] == 'Order cancelled.' || $dataArray['imgFile'][$i] == 'Order expired.' || $dataArray['imgFile'][$i] == 'Order was considered paid but not shipped.' || $dataArray['imgFile'][$i] == 'Order was shipped/picked up.' || $dataArray['imgFile'][$i] == 'Order was unpaid but was confirmed with deposit slip.')
				{
					//ARCHIVED
					$resetLink = '[<a href="'.INDEX.'orderList'.EXT.'archive=yes&orderstat=resetArchive&orderID='.$dataArray['orderID'][$i].'" class="orderResetArchiveLink"><b>Revert to Active Database</b></a>]';
					$imgLink = 'Archived/Cancelled when the '.strtolower($dataArray['imgFile'][$i]);
				}
				else
				{
					if(file_exists('img/a1ow17n1y437bawg42147/'.$dataArray['imgFile'][$i]))
					{
						//NOT YET PAID BUT DEPOSITSLIP IMAGE APPEARS
						$resetLink = '[<a class="orderPaidLink" href="'.INDEX.'orderList'.EXT.'orderstat=paid&orderID='.$dataArray['orderID'][$i].'"><b>Order Paid</b></a>] [<a class="orderResetApp" href="'.INDEX.'404" alt="'.INDEX.'orderList'.EXT.'orderstat=reset&orderID='.$dataArray['orderID'][$i].'"><b>Reset to Pending</b></a>] [<a class="orderDeleteLink" href="'.INDEX.'orderList'.EXT.'orderstat=del&orderID='.$dataArray['orderID'][$i].'"><b>Cancel</b></a>]';
						$imgLink = 'Confirmed. [<a href="./img/a1ow17n1y437bawg42147/'.$dataArray['imgFile'][$i].'" target="_blank">Deposit Slip</a>] ';
					}
					else
					{
						//PAID AND SHIPPED ORDERS
						$resetLink = '[<a class="orderDeleteLink" href="'.INDEX.'orderList'.EXT.'orderstat=del&orderID='.$dataArray['orderID'][$i].'"><b>Archive</b></a>]';
						$imgLink = 'Shipped: '.($dataArray['imgFile'][$i] == '0' ? 'Pickup at #7 Makaturing St.' : 'Waybill Number: '.$dataArray['imgFile'][$i]);
					}
					
				}
				
				
				$j = $i%2;
				
				
				$return .= '
				<div'.($j == 0 ? ' class="stripes"' : '').'>
					<div class="rows item'.(get('archive') == 'null' ? '' : ' archived').'" rel="'.$i.'">
						<h3>Sender: '.getMemberEmail($dataArray['memberID'][$i]).'</h3>
						<p>
							<b>Date: '.dateParser(explode('-', date('Y-m-d', strtotime('-3 days', strtotime($dateOnCall))))).'</b>
						</p>
					</div>
					<div id="target'.$i.'" class="targets">
						<h4>Details:</h4>
						'.$dataArray['orderDetails'][$i].'<br />
						<b>Status: '.$imgLink.'</b><br />
						<b>Tasks:</b> '.$resetLink.'<br /><br /> 
					</div>
				</div>
				';
			}
			return $return;
		}
	}
	
	function getOrderListDetails($allorOne, $id)
	{
		if($allorOne == 'all')
		{
			$tables = array();
			$tables[0] = 'ex_pending_orders';
			
			$fields = array();
			$fields[0] = 'orderID';
			$fields[1] = 'orderDetails';
			$fields[2] = 'memberID';
			$fields[3] = 'expiry';
			$fields[4] = 'hash';
			$fields[5] = 'imgFile';
			
			$whereClause = 'orderID > "0" ORDER BY orderID DESC, expiry DESC';
			
			$data = SQLRecover($tables, $fields, $whereClause);
			if($data['queryStatus'] !== 'DB Query success!')
			{
				return false;
			}
		}
		else if ($allorOne == 'one')
		{
			$tables = array();
			$tables[0] = 'ex_pending_orders';
			
			$fields = array();
			$fields[0] = 'orderID';
			$fields[1] = 'orderDetails';
			$fields[2] = 'memberID';
			$fields[3] = 'expiry';
			$fields[4] = 'hash';
			$fields[5] = 'imgFile';
			
			$whereClause = 'orderID="'.$id.'"';
			
			$data = SQLRecover($tables, $fields, $whereClause);
			if($data['queryStatus'] !== 'DB Query success!')
			{
				return false;
			}
		}
		else if($allorOne == 'archive')
		{
			$tables = array();
			$tables[0] = 'ex_orders_archive';
			
			$fields = array();
			$fields[0] = 'orderID';
			$fields[1] = 'orderDetails';
			$fields[2] = 'memberID';
			$fields[3] = 'expiry';
			$fields[4] = 'hash';
			$fields[5] = 'imgFile';
			
			$whereClause = 'orderID > "0" ORDER BY orderID DESC, expiry DESC';
			
			$data = SQLRecover($tables, $fields, $whereClause);
			if($data['queryStatus'] !== 'DB Query success!')
			{
				return false;
			}

		}
		else
		{
			return false;
		}
		return $data;
	}
	function displayPage()
	{
		cleanUpOrdersList();
		modifyOrder();
		
		$output = '
		<div id="navigators" class="container_12">
			<div class="grid_12 htop">
				<h2>'.(get('archive') == 'null'? 'Active Orders' : 'Inactive/Cancelled Orders').'</h2>
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			
			<div class="grid_1">
				&nbsp;
			</div>
			<div class="grid_10 top">
				'.table_ize(getOrderListDetails((get('archive') != 'null' ? 'archive' : 'all'), 'g')).'
			</div>
			<div class="grid_1">
				&nbsp;
			</div>
			'.(get('archive') != 'null' ? '
			<div class="clear">
			<!--spanner-->
			</div>
			
			<div class="grid_1">
				&nbsp;
			</div>
			<div class="grid_10 centerAlign">
				<input type="button" class="buttons" value="Save Archive" />
				<input type="button" class="buttons" value="Clear Archive" />
			</div>
			<div class="grid_1">
				&nbsp;
			</div>
			' : '').'
		</div>
		';
		return adminContent($output,'404 File Error: File Not Found.');
	}
?>